import { getGeocode } from '@/api/Amap/geo';
import { ElMessage } from 'element-plus'
import SourceVector from 'ol/source/vector';
import GeoJSON from 'ol/format/GeoJSON'
export function setGeoLayer(map, city) {
    //console.log(city)
  return getGeocode(city).then(res => {
        //console.log(res)
        if (res.status == 1) {
            const geo = res.geocodes[0];
            const adcode = geo.adcode;
            const location = geo.location.split(',');
            let center = [location[0], location[1]]
            let url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}.json`
            let geo_source = new SourceVector({
                url: url,
                format: new GeoJSON()
            })
            let vector = map.getLayers().getArray()[1]
            //console.log(vector)
            if (vector) {
                vector.setSource(null)
            }
            vector.setSource(geo_source)
            map.getView().animate({
                center: center,
                zoom: 9
            })
            console.log(geo.city)
            let cityName = geo.city
            return cityName
        } else {
            //console.log('获取地址失败')
            ElMessage.error('获取地址失败')
            return false
        }
    });

}